package cirno.open.api.enums;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 编程语言 cmd 枚举
 * 不需要编译的语言编译的 cmd 设置为空即可
 */
@Getter
public enum LanguageCmdEnum {
    JAVA("java", "Main.java", new String[]{"javac", "-encoding", "utf-8", "Main.java"}, new String[]{"java", "-Dfile.encoding=UTF-8", "Main"}),
    CPP("cpp", "main.cpp", new String[]{"g++", "-finput-charset=UTF-8", "-fexec-charset=UTF-8", "-o", "main", "main.cpp"}, new String[]{"./main"}),
    C("c", "main.c", new String[]{"gcc", "-finput-charset=UTF-8", "-fexec-charset=UTF-8", "-o", "main", "main.c"}, new String[]{"./main"}),
    PYTHON3("python", "main.py", new String[]{"python","-c", "\"compile(open('main.py').read(), 'main.py', 'exec')\""}, new String[]{"python3", "main.py"}),
    JAVASCRIPT("javascript", "index.js", new String[]{"npx","eslint","index.js"}, new String[]{"node", "index.js"}),
    TYPESCRIPT("typescript", "index.ts", new String[]{"npx","eslint", "index.ts"}, new String[]{"npx","tsc","index.ts","&&","node", "index.js"}),
    GO("go", "main.go", new String[]{"go","build","main.go"}, new String[]{"./main"})
    ;

    private final String language;

    /**
     * 保存的文件名
     */
    private final String saveFileName;

    private final String[] compileCmd;

    private final String[] runCmd;


    LanguageCmdEnum(String language, String saveFileName, String[] compileCmd, String[] runCmd) {
        this.language = language;
        this.saveFileName = saveFileName;
        this.compileCmd = compileCmd;
        this.runCmd = runCmd;
    }

    /**
     * 根据 language 获取枚举
     *
     * @param language 值
     * @return {@link LanguageCmdEnum}
     */
    public static LanguageCmdEnum getEnumByValue(String language) {
        if (StringUtils.isBlank(language)) {
            return null;
        }
        for (LanguageCmdEnum languageCmdEnum : LanguageCmdEnum.values()) {
            if (languageCmdEnum.language.equals(language)) {
                return languageCmdEnum;
            }
        }
        return null;
    }

    /**
     * 获取所有支持的编程语言
     * @return 所有支持的编程语言
     */
    public static List<String> getLanguages() {
        return Arrays.stream(LanguageCmdEnum.values()).map(LanguageCmdEnum::getLanguage).collect(Collectors.toList());
    }

    public String getLanguage() {
        return language;
    }

    public String getSaveFileName() {
        return saveFileName;
    }

    public String[] getCompileCmd() {
        return compileCmd;
    }

    public String[] getRunCmd() {
        return runCmd;
    }
}



